package com.kk.netty.basice.demo1;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.util.Date;

public class Client {
    public static void main(String[] args) throws InterruptedException {
        // 启动类
        new Bootstrap ( )
                // 添加 EventLoop
                .group (new NioEventLoopGroup ( ))
                // 选择客户端 channel 实现
                .channel (NioSocketChannel.class)
                // 添加处理器
                .handler (new ChannelInitializer<Channel> ( ) {
                    @Override
                    protected void initChannel(Channel ch) {
                        // 把hello 转化为 ByteBuf
                        ch.pipeline ( ).addLast (new StringEncoder ( ));
                    }
                })
                // 连接到服务器
                .connect ("127.0.0.1", 8080)
                // 阻塞方法，直到连接建立
                .sync ( )
                // 代表连接对象
                .channel ( )
                // 发送数据
                .writeAndFlush (new Date ( ) + ": hello world!");

    }
}
